package com.changer.common.service.impl;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.changer.common.service.JmsService;
import com.changer.common.vo.exception.InvalidArgumentException;

public class JmsServiceImpl implements JmsService {

	private JmsTemplate jmsTemplate;

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	public void send(final com.changer.jms.protocol.Message message) {
		if (this.jmsTemplate == null) {
			throw new InvalidArgumentException("jmsTemplate is null");
		}
		this.jmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				ObjectMessage om = session.createObjectMessage();
				om.setObject(message);
				return om;
			}
		});
	}
}
